package com.wclass.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wclass.common.ReturnObject;
import com.wclass.entity.UserAmountBill;
import com.wclass.entity.UserWithdrawBill;
import com.wclass.mapper.BaseAppUserMapper;
import com.wclass.mapper.UserAmountBillMapper;
import com.wclass.mapper.UserWithdrawBillMapper;
import com.wclass.service.IUserAmountBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;

/**
 * <p>
 * 用户佣金编辑 服务实现类
 * </p>
 *
 * @author 张浩高
 * @since 2018-12-20
 */
@Service
public class UserAmountBillServiceImpl extends ServiceImpl<UserAmountBillMapper, UserAmountBill> implements IUserAmountBillService {

    @Autowired
    private BaseAppUserMapper baseAppUserMapper;
    @Autowired
    private UserWithdrawBillMapper userWithdrawBillMapper;

    /**
     * 提现税点
     */
    @Value("${withdrawTax}")
    private Integer withdrawTax;

    /**
     * 最低提现金额
     */
    @Value("${withdrawLowest}")
    private Long withdrawLowest;


    /**
     * 提现申请
     *
     * @param userId
     * @param amount
     * @param cardholder
     * @param bankName
     * @param bankNo
     * @return
     */
    @Transactional
    @Override
    public ReturnObject withdrawalApply(Long userId, Long amount, String cardholder, String bankName, String bankNo) {

        if (amount == null || amount.longValue() < withdrawLowest.longValue()) {
            return new ReturnObject<>(1, "每次提现最低" + withdrawLowest);
        }
        if (baseAppUserMapper.deductionsAmount(userId, amount) > 0) {
            Long taxAmount = amount * withdrawTax / 100;
            UserWithdrawBill userWithdrawBill = new UserWithdrawBill(0, userId,null, bankName, bankNo, amount,
                    amount - taxAmount, withdrawTax, taxAmount, null, 0, null, cardholder,null);

            //增加提现申请
            userWithdrawBillMapper.insert(userWithdrawBill);
            //增加流水
            baseMapper.insert(new UserAmountBill(0, userId, userWithdrawBill.getId(), 1, amount, "提现:" + amount + " " +
                    "结余:" + baseAppUserMapper.selectById(userId).getAmount(), null, 2));
            //增加提现金额
            baseAppUserMapper.addWithdrawAmount(userId, amount);
            return new ReturnObject(0, "提现成功", userWithdrawBill);
        }
        return new ReturnObject(1, "提现失败");

    }



    /**
     * 提现成功
     *
     * @param userId
     * @param withdrawId
     * @return
     */
    @Transactional
    @Override
    public ReturnObject withdrawalSuccess(Long userId, Long withdrawId, Long payAmount) {
        //修改申请状态
        UserWithdrawBill userWithdrawBill = userWithdrawBillMapper.selectById(withdrawId);
        userWithdrawBill.setPayState(1);
        userWithdrawBill.setPayAmount(payAmount);
        userWithdrawBillMapper.updateById(userWithdrawBill);
        //修改流水状态
        UserAmountBill userAmountBill = baseMapper.selectList(new EntityWrapper<UserAmountBill>().eq("source",
                withdrawId)).get(0);
        userAmountBill.setChangeType(3);
        baseMapper.updateById(userAmountBill);
        return new ReturnObject(0, "提现成功");
    }


    /**
     * 提现失败
     *
     * @param userId
     * @param withdrawId
     * @return
     */
    @Transactional
    @Override
    public ReturnObject withdrawalFailure(Long userId, Long withdrawId) {
        UserWithdrawBill userWithdrawBill = userWithdrawBillMapper.selectById(withdrawId);
        if (userWithdrawBill.getPayState() != 0) {
            return new ReturnObject(1, "请不要重复操作");
        }
        //返还金额
        if (baseAppUserMapper.addAmount(userId, userWithdrawBill.getAmount()) > 0) {
            //修改申请状态
            userWithdrawBill.setPayState(2);
            userWithdrawBillMapper.updateById(userWithdrawBill);
            //修改流水状态
            UserAmountBill userAmountBill = baseMapper.selectList(new EntityWrapper<UserAmountBill>().eq("source",
                    withdrawId)).get(0);
            userAmountBill.setChangeType(4);
            baseMapper.updateById(userAmountBill);
            //增加佣金变动流水
            baseMapper.insert(new UserAmountBill(0, userId, userWithdrawBill.getId(), 2, userWithdrawBill.getAmount()
                    , "提现失败，退还收益" + userWithdrawBill.getAmount() + " 结余:" +
                    baseAppUserMapper.selectById(userId).getAmount(), null, 1));
            //减少提现金额
            baseAppUserMapper.deductionsWithdrawAmount(userId, userWithdrawBill.getAmount());
            return new ReturnObject(0, "收益退还成功");
        }
        return new ReturnObject(1, "收益退还失败");
    }
}
